<?php
if(!defined('DCS_ROOT')) {exit('Access Denied');} 

class member {

	function __construct (){
		$this->url = getUrl();
		$action = $this->url['params']['1'];
		require_once(APP_PATH."model/admin/init.php" );
		if($action == 'add'){
			$this->add();
			exit();
		}elseif($action == 'edit'){
			$this->edit();
			exit();
		}elseif($action == 'del'){
			$this->del();
			exit();
		}else{
			$this->defaultAction();
			exit();
		}
	}	

	function defaultAction(){
		$tpl = new Template( "admin/member/list.htm" );
		require_once(APP_PATH."model/admin/member.php" );
		$DB  = getDB();
		$data = new MemberModel ($DB);
		if($this->url['params']['1'] == 'page'){
				$pagetemp = $this->url['params']['2'];		
		}
		$condition['page']= is_numeric($pagetemp) ? $pagetemp :'1';
		$condition['rows'] = '20';
		$condition['order'] = 'regtime';
		if(isPost()){
			$keyword = htmlClear(addslashes(trim($_POST['keyword'])));
			if (strlen($keyword) < 2) {
				message('关键字不能少于2个字符！',2);
			}
			if(!empty($keyword)){
				$condition['condition'] = " username LIKE '%$keyword%' OR email LIKE '%$keyword%' ";
			}else{
				message('请输入要搜索的关键字！',2);
			}			
			$condition['rows'] = '200';
		}
		$memberList = $data->getList($condition);
		foreach($memberList as $k => $v ){
			$memberList[$k]['lasttime'] = date("Y-m-d  H:i:s",$v['lasttime']);
			$memberList[$k]['regtime'] = date("Y-m-d  H:i:s",$v['regtime']);
			$memberList[$k]['username'] = highlightWords($v['username'],$keyword);
		}
		$tpl->assign('list', $memberList);
		$condition['pageUrl'] = '/admin/member/page/';
		$condition['pageTotal']	= $data->total($condition);
		$tpl->assign("pageParam", makePage($condition));
		$tpl->output();
	}

	function add(){
		require_once(APP_PATH."model/admin/member.php" );
		$DB  = getDB();
		$data = new MemberModel( $DB );
		if(!isPost()){
			$tpl = new Template( "admin/member/add.htm");		
			$tpl->output();
		}else{
			foreach($_POST as $k => $v ){				
				$art[$k] = daddslashes(trim($v));
			}			
			$art['password'] = md5($art['password']);
			
			global $timestamp,$onlineip;
			$art['regip'] = $onlineip;
			$art['regtime'] = $timestamp;
			$art['lastip'] = $onlineip;
			$art['lasttime'] = $timestamp;

			if(!empty($_FILES['avatars']['name']) ){
				require_once(CLASS_PATH."class/upload.class.php" );
				$upload = new Upload();
				$upload->config(array(
					'targetDir' => '../avatars/',
					'saveType' => 0,
					'thumbStatus' => 0,
					'waterMarkStatus' => 0
				));
				$files = $upload->saveFiles('avatars');
				$art['avatars'] = $files['0']['filenames'];
			}
			$add = $data->addOne($art);
			if ($add){
				$message = '成功增加用户';
			}else{
				$message = '增加用户失败';
			}
			message($message);	
		}
	}

	function edit(){
		require_once(APP_PATH."model/admin/member.php" );
		$DB  = getDB();
		$data = new MemberModel ($DB);
		$id = $this->url['params']['2'];
		if(!is_numeric($id)){
			message('找不到此条信息。');
		}
		$member = $data->getMember($id);
		if(!isPost()){
			$tpl = new Template("admin/member/edit.htm" );			
			$member['lasttime'] = date('y-m-d H:i:s',$member['lasttime']);
			$member['regtime'] = date('y-m-d H:i:s',$member['regtime']);
			$member['lockuser'] = $member['lockuser'] == '1' ? 'checked' : '';
			$member['avatars2'] = empty($member['avatars']) ? '' : '<br><img src="'.PUB.'avatars/'.$member['avatars'].'" width="80">';
			$tpl->assign($member);
			$tpl->output();
		}else{
			$art['username'] = trim($_POST['username']);
			$art['password'] = trim($_POST['password']);
			$art['email'] = trim($_POST['email']);
			$art['lockuser'] = trim($_POST['lockuser']);
			$art['avatars'] = trim($_POST['avatars']);			
			if(!empty($_FILES['avatars']['name'])){
				require_once(CLASS_PATH."class/upload.class.php" );
				$upload = new Upload();
				$upload->config(array(
					'targetDir' => '../avatars/',
					'saveType' => 0,
					'thumbStatus' => 0,
					'waterMarkStatus' => 0
				));
				$files = $upload->saveFiles('avatars');
				$art['avatars'] = $files['0']['filenames'];
			}else{
				$art['avatars'] = $member['avatars'];
			}

			$edit = $data->editOne($id,$art['username'],$art['password'],$art['email'],$art['lockuser'],$art['avatars']);
			if ($edit){
				$message = '修改用户成功。';
			}else{
				$message = '修改用户失败！';
			}
			message($message);			
		}
	}

	function del(){
		require_once(APP_PATH."model/admin/member.php" );
		$DB  = getDB();
		$data = new MemberModel( $DB );
		$id = $this->url['params']['2'];
		if(!is_numeric($id)){
			error('找不到此条信息。');
		}				
		$del = $data->delOne($id);
		if ($del){
			$message = '删除用户成功。';
		}else{
			$message = '删除用户失败！';
		}
		message($message);		
	}

}
?>
